package com.cysion.study.ui.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.content.res.ResourcesCompat
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.base.BaseFragmentAdapter
import com.cysion.study.ktbox.utils.CLog
import com.cysion.targetfun._addOnPageChangeListener
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.AlertSearch
import com.cysion.study.R
import com.cysion.study.adapter.ClassPushManageAdapter
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.personal.ClassPushDetailActivity
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_classpush_manage_list.*
import java.util.*

/**
 * 普通教师-课程推广
 * Created by chu on 2019-06-30.
 */
class LeveManageFragment:BaseFragment(){
    override fun getLayoutId(): Int {
        return R.layout.activity_classpush_manage_list
    }

    private lateinit var dataAdapter: ClassPushManageAdapter
    private val dataList: MutableList<String> = mutableListOf()
    private var curPage = 1

    override fun initView() {
//        darkTextTheme(color(R.color.white))
//        darkTextTheme(color(R.color.main_cl))
        topbar.apply {
            initElements(TopBar.ELEMENT.HIDE,TopBar.ELEMENT.TEXT,TopBar.ELEMENT.HIDE)
            setTitle("课程推广")
        }
        initRefreshLayout()
        initRecyclerView()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList()
            smartLayout.setEnableLoadMore(true)

        }
        smartLayout.setOnLoadMoreListener {
            getDataList()
        }

    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        dataAdapter = ClassPushManageAdapter(dataList, Box.context)
        rv_list.adapter = dataAdapter
        rv_list.layoutManager = LinearLayoutManager(Box.context)
        dataAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {//预览
                /* var bundle = Bundle()
                 bundle.putString("orderId", obj.id)
                 startActivity_ex<ClassRecordActivity>("key", bundle)*/
                UIHelper.jump(activity,ClassPushDetailActivity::class.java)//推广课程详情
            }
        }

        //模拟数据
        /*if (dataAdapter.itemCount == 0) {
            var lists: MutableList<String> = mutableListOf("","")
            dataList.addAll(lists)
            dataAdapter.notifyDataSetChanged()
        }*/
    }

    private var startRefresh=true
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            curPage = 1
            if (startRefresh) {
                getDataList()
                startRefresh=false
            }
        }
    }

    private fun getDataList() {
        Alert.loading(context)
        ApiClient.api.getTutorOrderList(UserCache.token,"",curPage.toString()).compose(
            BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    multiView.showEmpty()
                    /*if (curPage == 1) {
                        dataList.clear()
                        if (dataList.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        dataAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }*/
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    stopRefresh()
                }
            }

    }

    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }
    override fun closeMvp() {
    }
}